package com.example.springboot.utils;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;

import java.io.InputStream;

/**
 * @auther: Wyz
 * @date: 2023/7/11 10:45
 * @return
 */
public class WordToPdf {


    public static boolean getLicense(String classPath) {
        boolean result = false;
        try {
            InputStream is = WordToPdf.class.getResourceAsStream("/license.xml");
            Class clazz = Class.forName(classPath);
            Object obj = clazz.newInstance();
            clazz.getMethod("setLicense", InputStream.class).invoke(obj, is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


        public static void main(String[] args) {
            // 验证License
            if (!getLicense("com.aspose.words.License")) {
                return;
            }
            String inputFilePath = "D:\\zhuomian\\input.docx"; // Word文件路径
            String outputFilePath = "D:\\zhuomian\\output.pdf"; // 输出PDF文件路径

            try {
                // 加载Word文档
                Document doc = new Document(inputFilePath);

                // 将Word文档保存为PDF
                doc.save(outputFilePath, SaveFormat.PDF);

                System.out.println("Word转PDF成功！");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
}
